package com.usermanage.dao.impl;

import java.sql.SQLException;
import java.util.Map;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.MapHandler;

import com.usermanage.bean.Admin;
import com.usermanage.dao.AdminDao;
import com.usermanage.tool.DBTool;

/**
 * 数据库访问接口
 * @author 陆贝伊
 *
 */
public class AdminDaoImol implements AdminDao{
	/**
	 * 查找用户
	 */
	@Override
	public Admin queryAdminByAccount(Admin a) {
		System.out.println("查询的用户为："+a);
		String sql="select id,account,password,name from admin where account=?";
		Admin admin=null;
		try {

				admin=DBTool.qr.query(sql, new BeanHandler<Admin>(Admin.class), a.getAccount());

		} catch (Exception  e) {
			e.printStackTrace();
		}
		if (admin!=null) {
			System.out.println("查询到后台信息为："+admin);
		}else {
			admin=new Admin();
			admin.setAccount("0");
			admin.setPassword("0");
		}
		
		return admin;

	}
	/**
	 * 判断重复
	 */
	@Override
	public Integer queryRepeatByAccount(String account) {
		System.out.println("查询的账号为："+account);
		String sql="select id,account,password,name from admin where account=?";
		Admin admin=null;
		Integer res=0;
		try {
			admin=DBTool.qr.query(sql, new BeanHandler<Admin>(Admin.class), account);
		} catch (Exception  e) {
			e.printStackTrace();
		}
		if (admin!=null) {
			System.out.println("查询到后台信息为："+admin);
			res=1;
		}
		
		return res;
	}
	/**
	 * 注册用户
	 * @param a
	 * @return
	 */
	@Override
	public int addAdmin(Admin a) {
		String sql="insert into admin(account,password,name)value(?,?,?)";
		int res=0;
		try {
			res=DBTool.qr.update(sql, a.getAccount(),a.getPassword(),a.getName());
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return res;
	}

}
